package com.dy.drp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.drp.entity.Log;
import com.dy.drp.service.ILogService;
import com.dy.drp.util.SelectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dy
 * @since 2022-04-01
 */
@RestController
@RequestMapping("/log")
@Api(tags = "日志一览接口")
public class LogController {
    @Autowired
    private ILogService logService;

    @GetMapping("/insertLog/{userName}")
    @ApiOperation("添加日志")
    @ApiImplicitParam(name = "userName",value = "用户名")
    public String insertLog(@PathVariable("userName")String userName){
        DateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd");
        Log log = new Log();
        log.setLogTime(dfDate.format(new Date()));

        DateFormat dfTime = new SimpleDateFormat("HH:mm:ss");
        String content = "用户"+userName+"在"+dfTime.format(new Date())+"登录系统";
        log.setLogContent(content);
        boolean save = logService.save(log);
        if (save){
            return "添加成功";
        }
        return "添加失败";
    }

    @GetMapping("/selectOnePageLog/{pageNum}/{pageSize}")
    @ApiOperation("查询单页日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNum",value="当前页码"),
            @ApiImplicitParam(name = "pageSize",value = "页面展示条数")
    })
    public Page<Log> selectOnePageLog(@PathVariable("pageNum")Integer pageNum,
                                      @PathVariable("pageSize")Integer pageSize){
        Page<Log> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Log> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("log_id");
        return logService.page(page, wrapper);
    }

    @PostMapping("/selectOnePageLogByInfo")
    @ApiOperation("查询单页日志通过日期")
    public Page<Log> selectOnePageLogByInfo(SelectUtil selectUtil){
        Page<Log> page = new Page<>(selectUtil.getPageNum(),selectUtil.getPageSize());
        QueryWrapper<Log> wrapper = new QueryWrapper<>();
        wrapper.between("log_time",selectUtil.getStartTime(),selectUtil.getEndTime());
        wrapper.orderByDesc("log_id");
        return logService.page(page, wrapper);
    }


}
